import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/11 8:57
 * @description 不太懂
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] parents = {-1, 2, 0, 2, 0};
        solution.countHighestScoreNodes(parents);
    }
    long maxScore = 0;
    int cnt = 0;
    int n;
    List<Integer>[] children;
    public int countHighestScoreNodes(int[] parents) {
        n = parents.length;
        children = new List[n];
        for (int i = 0; i < n; i++) {
            children[i] = new ArrayList<>();
        }
        for (int i = 1; i < n; i++) {
            int p = parents[i];
            children[p].add(i);
        }
        dfs(0);
        return cnt;
    }
    public int dfs(int node) {
        long score = 1;
        int size = n - 1;
        for (int c: children[node]) {
            int t = dfs(c);
            score *= t;
            size -= t;
        }
        if (node != 0) {
            score *= size;
        }
        if (score == maxScore) {
            cnt++;
        }else if (score > maxScore) {
            maxScore = score;
            cnt=1;
        }
        return n-size;
    }
}
